<template>
  <div class="article-detail">
    <div v-if="article" class="article-content">
      <div class="article-header">
        <h1>{{ article.title }}</h1>
        <div class="article-actions" v-if="isAuthenticated">
          <router-link :to="`/article/edit/${article.id}`" class="btn-edit">编辑</router-link>
          <button @click="handleDelete" class="btn-delete">删除</button>
        </div>
      </div>
      <div class="article-meta">
        <span class="meta-item">
          <i class="icon">📂</i>
          分类：{{ article.category }}
        </span>
        <span class="meta-item">
          <i class="icon">📅</i>
          发布时间：{{ formatDate(article.createTime) }}
        </span>
        <span class="meta-item">
          <i class="icon">🔄</i>
          更新时间：{{ formatDate(article.updateTime) }}
        </span>
      </div>
      <div class="article-summary">
        <h3>文章简介</h3>
        <p>{{ article.summary }}</p>
      </div>
      <div class="article-content-main">
        <h3>正文内容</h3>
        <div class="content">{{ article.content }}</div>
      </div>
    </div>
    <div v-else class="loading">
      <span>加载中...</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { getBlogById, deleteBlog } from '../api/blog'

const route = useRoute()
const router = useRouter()
const store = useStore()
const article = ref(null)
const isAuthenticated = computed(() => store.state.token)

const fetchArticle = async () => {
  try {
    const response = await getBlogById(route.params.id)
    if (response.code === 200) {
      article.value = response.data
    } else {
      console.error('获取文章详情失败：', response.msg)
    }
  } catch (error) {
    console.error('获取文章详情失败：', error)
  }
}

const handleDelete = async () => {
  if (!confirm('确定要删除这篇文章吗？')) return
  
  try {
    await deleteBlog(route.params.id)
    alert('删除成功')
    router.push('/')
  } catch (error) {
    alert('删除失败：' + error.message)
  }
}

const formatDate = (date) => {
  return new Date(date).toLocaleString()
}

onMounted(() => {
  fetchArticle()
})
</script>

<style scoped>
.article-detail {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

h1 {
  margin: 0;
  font-size: 28px;
  color: #2c3e50;
}

.article-actions {
  display: flex;
  gap: 10px;
}

.article-meta {
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 30px;
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #666;
  font-size: 14px;
}

.icon {
  font-size: 16px;
}

.article-summary {
  margin-bottom: 30px;
  padding: 20px;
  background-color: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
}

.article-summary h3 {
  margin-top: 0;
  color: #2c3e50;
  font-size: 18px;
}

.article-summary p {
  margin: 10px 0 0;
  color: #666;
  line-height: 1.6;
}

.article-content-main {
  background-color: #fff;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}

.article-content-main h3 {
  margin-top: 0;
  color: #2c3e50;
  font-size: 18px;
}

.content {
  margin-top: 15px;
  line-height: 1.8;
  color: #2c3e50;
  white-space: pre-wrap;
  font-size: 16px;
}

.btn-edit, .btn-delete {
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.btn-edit {
  background-color: #2196F3;
  color: white;
  text-decoration: none;
}

.btn-delete {
  background-color: #f44336;
  color: white;
  border: none;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #666;
}
</style> 